﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using Public;
using BLL;

namespace QLNhanKhau
{
    public partial class frmNguoiDung : DevComponents.DotNetBar.Metro.MetroForm
    {
        public frmNguoiDung()
        {
            InitializeComponent();
        }

        private void frmNguoiDung_Load(object sender, EventArgs e)
        {
            cmbChucVuThem.DataSource = ChucVuBLL.LoadChucVu();
            cmbChucVuThem.DisplayMember = "TenChucVu";
            cmbChucVuThem.ValueMember = "MaCV";

            cmbChucVu.DataSource = ChucVuBLL.LoadChucVu();
            cmbChucVu.DisplayMember = "TenChucVu";
            cmbChucVu.ValueMember = "MaCV";

            dgvNguoiDung_Load();

        }

        private void dgvNguoiDung_Load()
        {
            DataGridViewCellStyle style = new DataGridViewCellStyle();
            style.ForeColor = Color.AliceBlue;
            style.BackColor = Color.Red;

            List<NguoiDungPublic> list_nd = NguoiDungBLL.LoadToanBoNguoiDung();
            dgvNguoiDung.DataSource = null;
            dgvNguoiDung.DataSource = list_nd;

            dgvNguoiDung.Columns[0].HeaderText = "Mã người dùng";
            //dgvNguoiDung.Columns[1].HeaderText = "Mã nhân khẩu";
            dgvNguoiDung.Columns[1].HeaderText = "Tên đăng nhập";
            dgvNguoiDung.Columns[2].HeaderText = "Mật khẩu";
            dgvNguoiDung.Columns[3].HeaderText = "Họ Tên";
            dgvNguoiDung.Columns[4].HeaderText = "Chức vụ";

            dgvNguoiDung.Columns[5].HeaderText = "Tình trạng";
            dgvNguoiDung.Columns[6].HeaderText = "Ngày sinh";


            //dgvNguoiDung.Columns[6].HeaderText = "Số điện thoại";

            //dgvNguoiDung.Columns[0].Visible = false;
            //dgvNguoiDung.Columns[1].Visible = false;
            dgvNguoiDung.Columns[2].Visible = false;
            //dgvNguoiDung.Columns[3].Visible = false;
            //dgvNguoiDung.Columns[4].Visible = false;
            //dgvNguoiDung.Columns[5].Visible = false;
            txtMaNDThem.Text = DungChung.LoadMaND().ToString();
            dgvNguoiDung.Columns[6].Visible = false;

            for (int i = dgvNguoiDung.RowCount - 1; i >= 0; i--)
            {
                if ((bool)dgvNguoiDung.Rows[i].Cells[5].Value == false)
                {
                    dgvNguoiDung.Rows[i].DefaultCellStyle = style;
                }
            }

        }

        private void dgvNguoiDung_SelectionChanged(object sender, EventArgs e)
        {
            
            DataGridViewCell cell = null;
            foreach (DataGridViewCell select in dgvNguoiDung.SelectedCells)
            {
                cell = select;
            }
            if (cell != null)
            {
                DataGridViewRow row = cell.OwningRow;
                txtMaNguoiDung.Text = (string)row.Cells[0].Value.ToString();
               
                txtTenDangNhap.Text = (string)row.Cells[1].Value;
                txtMatKhau.Text = (string)row.Cells[2].Value;
                txtHoTen.Text = (string)row.Cells[3].Value;
                
                cmbChucVu.SelectedValue = row.Cells[4].Value;
                //if ((int)cmbChucVu.SelectedValue == (int)row.Cells[4].Value)
                //{ 
                //    cmbChucVu.SelectedValue==
                //}
                 //.Text = (string)row.Cells[4].Value;
                if ((bool)row.Cells[5].Value ==true)
                {
                    labTinhTrang.Text = "Đang hoạt động";
                    sbtTinhTrang.Value = true;
                }
                else
                {
                    labTinhTrang.Text = "Ngừng hoạt động";
                    sbtTinhTrang.Value = false;
                }
            }
            
        }

      

        private void btnLuuThem_Click(object sender, EventArgs e)
        {
            if (txtHoTen.Text == "" || txtTenDangNhap.Text == "")
            { 
                
            }
            NguoiDungPublic nd = new NguoiDungPublic();
            nd.MaND = int.Parse(txtMaNDThem.Text);
            nd.HoTen = txtHoTenThem.Text;
            nd.MatKhau = DungChung.md5("123456");
            nd.TenDangNhap = txtTenDangNhapThem.Text;
            nd.MaCV = (byte)cmbChucVuThem.SelectedValue;
            nd.TinhTrang = (bool)sbtTinhTrangThem.Value;

            NguoiDungBLL.InsertNguoiDung(nd);
            MessageBox.Show("Thêm thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            frmNguoiDung_Load(sender, e);
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            DialogResult result = MessageBox.Show("Thoát không lưu", "Thoát", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
            if (result == DialogResult.Yes)
            {
                this.MdiParent = null;
                this.Dispose();
            }
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
          //  NguoiDungBLL.UpdateTinhTrang(int.Parse(txtMaNguoiDung.Text), sbtTinhTrang.Value);

            frmNguoiDung_Load(sender, e);

            //btnTim_Click(sender, e);
        }

        private void dgvNguoiDung_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = true;
        }

       

      
    }
}
